Ext.define('Admin.view.employee.EmployeeViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.employeeViewController',
      
    search: function(view, recIndex, cellIndex, item, e, record) {
    var searchField = this.lookupReference('searchFieldName').getValue();
    var searchValue = this.lookupReference('searchFieldValue').getValue();
    
    var store = Ext.getCmp('employeeGridPanel').getStore();
    
     Ext.apply(store.proxy.extraParams, {empName:'',userName:'', sex:'', political:'', postId:null, partId:null, education:'', joinTime:null});
      
    if(searchField==='empName'){
      Ext.apply(store.proxy.extraParams, {empName:searchValue});
    }
    if(searchField==='userName'){
      Ext.apply(store.proxy.extraParams, {userName:searchValue});
    }
    
    store.load({params:{start:0, limit:25, page:1}});
    
    }
  ,openMoreSearchWindow: function(view, recIndex, cellIndex, item, e, record) {
           var cfg = Ext.apply({
                xtype: 'employeeEditWindow',
                items: [Ext.apply({xtype: 'employeeSearchForm'})]
             });
          var win = Ext.create(cfg);
    }
    ,moreSearch: function(btn) {
          var form=btn.up('form');
          
           var store = Ext.getCmp('employeeGridPanel').getStore();
           Ext.apply(store.proxy.extraParams, {empName:'',userName:'', sex:'', political:'', postId:null, partId:null, education:'', joinTime:null});
           Ext.apply(store.proxy.extraParams, {
            empName:form.lookupReference('employeeSearchEmpName').getValue(),
            sex:form.lookupReference('employeeSearchSex').getValue(),
            political:form.lookupReference('employeeSearchPolitical').getValue(),
            postId:form.lookupReference('employeeSearchPost').getValue(),
            partId:form.lookupReference('employeeSearchPart').getValue(),
            education:form.lookupReference('employeeSearchEducation').getValue(),
            joinTime:Ext.util.Format.date(form.lookupReference('employeeSearchJoinTime').getValue(), 'Y/m/d H:i:s')
          } );
        store.load({params:{start:0, limit:25, page:1}});
        Ext.getCmp('employeeSearchFieldValue').setValue('');
        btn.up('window').close();
    }
    ,openAddWindow: function() {
              var cfg = Ext.apply({
                xtype: 'employeeEditWindow',
                items: [Ext.apply({xtype: 'employeeAddForm'})]
    });
    var win = Ext.create(cfg);
    },submitAddForm: function(btn) {
          var form = btn.up('form');
      form.getForm().submit({       
        url:'employee/saveOrUpdate',
        //waitMsg: '正在上传，请耐心等待....',
        success: function(view, action){    
          Ext.Msg.alert('提示', action.result.msg,function(){
            btn.up('window').close();
            //form.getViewModel().getStore('employStore').reload();
                      Ext.getCmp('employeeGridPanel').getStore().reload();
          });       
        },       
        failure: function(view, action){
          Ext.Msg.alert('提示', action.result.msg);
        }
      });
    },openEditWindow: function(view, recIndex, cellIndex, item, e, record) {
      var cfg = Ext.apply({
                xtype: 'employeeEditWindow',
                items: [Ext.apply({xtype: 'employeeEditForm'})]
    });
    var win = Ext.create(cfg);
    
    win.down("form").getForm().loadRecord(record);//加载修改数据到Form
    
        
    },submitEditForm: function(btn) {
          var form = btn.up('form');
      form.getForm().submit({       
        url:'employee/saveOrUpdate',
        //waitMsg: '正在上传，请耐心等待....',
        success: function(view, action){    
          Ext.Msg.alert('提示', action.result.msg,function(){
            btn.up('window').close();
            //form.getViewModel().getStore('employStore').reload();
                      Ext.getCmp('employeeGridPanel').getStore().reload();
          });       
        },       
        failure: function(view, action){
          Ext.Msg.alert('提示', action.result.msg);
        }
      });
    }
    ,deleteEmployee: function(view, recIndex, cellIndex, item, e, record) {
          Ext.MessageBox.confirm('提示', '确定要进行删除操作吗？数据将无法还原！',
            function(btn, text){
                  if(btn=='yes'){
                      Ext.Ajax.request({ 
                url : 'employee/delete', 
                method : 'post', 
                params : { 
                  empId :record.get('empId')
                }, 
                success: function(response, options) {
                          var json = Ext.util.JSON.decode(response.responseText);
                        if(json.success){
                          Ext.Msg.alert('系统提示', json.msg, function() {
                                view.getStore().reload();
                                Ext.getCmp('transferGridPanel').getStore().reload();
                            });
                      }else{
                         Ext.Msg.alert('系统提示', json.msg);
                      }
                      }
              });
                  }
              }
            , this);
    },
    deleteEmployees: function(btn) {
        var grid = btn.up('gridpanel');
    var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
                if (button == "yes") {
                    var rows = selModel.getSelection();
                    var selectIds = []; //要删除的id
                    Ext.each(rows, function (row) {
                        selectIds.push(row.data.empId);
                    });
                    Ext.Ajax.request({ 
            url : 'employee/deleteEmployees', 
            method : 'post', 
            params : { 
              //ids[] :selectIds
              ids :selectIds
            }, 
            success: function(response, options) {
                      var json = Ext.util.JSON.decode(response.responseText);
                    if(json.success){
                      Ext.Msg.alert('操作成功', json.msg, function() {
                            grid.getStore().reload();
                            Ext.getCmp('transferGridPanel').getStore().reload();
                        });
                  }else{
                     Ext.Msg.alert('操作失败', json.msg);
                  }
                  }
          });

                }
            });
        }
        else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
        }
    },
    employeeTip:function( view, record, item, index, e, eOpts ){
      if (view.tip == null) {  //这块判断很重要，不能每次都创建个tooltip，要不显示会有问题。
                view.tip = Ext.create('Ext.tip.ToolTip', {
                    // The overall target element.
                    target: view.el,
                    // Each grid row causes its own separate show and hide.
                    delegate: view.itemSelector,
                    // Moving within the row should not hide the tip.
                    //  trackMouse: false,
                    // Render immediately so that tip.body can be referenced prior to the first show.
                    renderTo: Ext.getBody()

                });
            };
           
             view.el.clean();
             var thisData=record.data;
             view.tip.update('<table><caption>其他信息</caption><tr><td valign="top"><img src="'+thisData.idcardImg+'" width="120" height="140" /></td>'+'<td valign="top">出生日期:'+Ext.Date.format(thisData.birthday,'Y/m/d')+'<br>籍贯:'+thisData.bornPlace+'<br>政治面貌:' + thisData.political + '<br>学历:' + thisData.education + '<br>电话:' + thisData.phone+'<br>邮箱:'+thisData.email+'</td></tr></table>');
   },
   openPhotoWindow:function() {
              var cfg = Ext.apply({
                xtype: 'photoWindow',
                items: [Ext.apply({xtype: 'photoForm'})]
    });
    var win = Ext.create(cfg);
    }
});